package gdut.jmxywz.action;

import gdut.jmxywz.been.ClassDown;
import gdut.jmxywz.been.Stclass;
import gdut.jmxywz.been.Student;
import gdut.jmxywz.been.Teacher;
import gdut.jmxywz.been.TeachingFiles;
import gdut.jmxywz.util.PageModel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

public class TeachingFilesAction extends BaseAction {
	
	public static final String TEACHER_SESSION_KEY = "teacherSessionKey";
	
	public static final String STUDENT_SESSION_KEY="studentSessionKey"; 
	
	private TeachingFiles teachingFiles;
	
	private Teacher teacher;
	
	private Student student;
	
	private Stclass stclass;
	
	private ClassDown classDown;
	
	private int teachSrcId;
	
	private int stclassId;
	
	private int[] stclasses;

	private String teachSrcfileName;
	
	private String teachSrcFileContext;
	
	private List teachingFilesList;
	
	private List stclassNotSelectedList;
	
	private List stclassSelectedList;
	
	private List classDownSelectedList;
	
	private String types;
	
/*---personalType是一个标志，用于区别不同权限的用户调用相同的action方法时的不同执行过程---*/	
	private int personalType=0;
	
	private int teacherId;
	
	private int studentId;

/*----------------审核类型和状态----------------------*/
	private int auditType;
	
	private int auditState=5;
	
/*----------------分页----------------------*/
	private PageModel pageModel;
	
	private int currentPage=1;
	
	private int pageSize=5;

/*----------------检索----------------------*/
	private String searchSelect="";
	
	private String like="like";
	
	private String searchValue="";
	
/*-----------------上传---------------------*/	
	private File uploadFile;
	
	private String uploadFileFileName;
	
	private String uploadFileContentType;

/*----------------下载----------------------*/
	private String fileDownSaveName;
	
	private String fileUrl;
	
	private InputStream inputStream;
	
	private String contentType;
	
/*======================setter和getter方法==========================*/
	public File getUploadFile() {
		return uploadFile;
	}

	public void setUploadFile(File uploadFile) {
		this.uploadFile = uploadFile;
	}

	public String getTeachSrcfileName() {
		return teachSrcfileName;
	}

	public void setTeachSrcfileName(String teachSrcfileName) {
		this.teachSrcfileName = teachSrcfileName;
	}

	public String getTeachSrcFileContext() {
		return teachSrcFileContext;
	}

	public void setTeachSrcFileContext(String teachSrcFileContext) {
		this.teachSrcFileContext = teachSrcFileContext;
	}
	
	public Teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
	
	

	public String getUploadFileFileName() {
		return uploadFileFileName;
	}

	public void setUploadFileFileName(String uploadFileFileName) {
		this.uploadFileFileName = uploadFileFileName;
	}

	public String getUploadFileContentType() {
		return uploadFileContentType;
	}

	public void setUploadFileContentType(String uploadFileContentType) {
		this.uploadFileContentType = uploadFileContentType;
	}
	
	public TeachingFiles getTeachingFiles() {
		return teachingFiles;
	}

	public void setTeachingFiles(TeachingFiles teachingFiles) {
		this.teachingFiles = teachingFiles;
	}
	
	public List getTeachingFilesList() {
		return teachingFilesList;
	}

	public void setTeachingFilesList(List teachingFilesList) {
		this.teachingFilesList = teachingFilesList;
	}
	
	public PageModel getPageModel() {
		return pageModel;
	}

	public void setPageModel(PageModel pageModel) {
		this.pageModel = pageModel;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	public String getSearchSelect() {
		return searchSelect;
	}

	public void setSearchSelect(String searchSelect) {
		this.searchSelect = searchSelect;
	}

	public String getLike() {
		return like;
	}

	public void setLike(String like) {
		this.like = like;
	}

	public String getSearchValue() {
		return searchValue;
	}

	public void setSearchValue(String searchValue)throws UnsupportedEncodingException {
		this.searchValue = new String(searchValue.getBytes("ISO-8859-1"),"utf-8");
	}
	
	public Stclass getStclass() {
		return stclass;
	}

	public void setStclass(Stclass stclass) {
		this.stclass = stclass;
	}
	
	
	public int getTeachSrcId() {
		return teachSrcId;
	}

	public void setTeachSrcId(int teachSrcId) {
		this.teachSrcId = teachSrcId;
	}

	public List getStclassNotSelectedList() {
		return stclassNotSelectedList;
	}

	public void setStclassNotSelectedList(List stclassNotSelectedList) {
		this.stclassNotSelectedList = stclassNotSelectedList;
	}

	public List getStclassSelectedList() {
		return stclassSelectedList;
	}

	public void setStclassSelectedList(List stclassSelectedList) {
		this.stclassSelectedList = stclassSelectedList;
	}
	
	public ClassDown getClassDown() {
		return classDown;
	}

	public void setClassDown(ClassDown classDown) {
		this.classDown = classDown;
	}
	
	public String getTypes() {
		return types;
	}

	public void setTypes(String types) {
		this.types = types;
	}
	
	public List getClassDownSelectedList() {
		return classDownSelectedList;
	}

	public void setClassDownSelectedList(List classDownSelectedList) {
		this.classDownSelectedList = classDownSelectedList;
	}
	
	public int getStclassId() {
		return stclassId;
	}

	public void setStclassId(int stclassId) {
		this.stclassId = stclassId;
	}
	
	public int[] getStclasses() {
		return stclasses;
	}

	public void setStclasses(int[] stclasses) {
		this.stclasses = stclasses;
	}
	
	public String getFileDownSaveName() {
		return fileDownSaveName;
	}

	public void setFileDownSaveName(String fileDownSaveName) {
		this.fileDownSaveName = fileDownSaveName;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	
	
	public String getFileUrl() {
		return fileUrl;
	}

	public void setFileUrl(String fileUrl) {
		this.fileUrl = fileUrl;
	}
/*匹配文件类型*/
	public String getContentType(String contentType) {
		for( String ext : contentTypes.keySet() ) {
			if( fileUrl.toLowerCase().endsWith( ext ) ) {
				return contentTypes.get( ext );
			}
		}
		return "application/octet-stream";
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

/*列出可能用到的文件格式匹配类型，用于getContentType(String contentType)中匹配*/

	static Map<String, String> contentTypes = new HashMap<String, String>();
	static {
		
		contentTypes.put("docx", "application/msword");
		contentTypes.put("xlsx", "application/vnd.ms-excel");
		contentTypes.put("pptx", "application/vnd.ms-powerpoint");
		contentTypes.put("doc", "application/msword");
		contentTypes.put("xls", "application/vnd.ms-excel");
		contentTypes.put("ppt", "application/vnd.ms-powerpoint");
		contentTypes.put("pdf", "application/pdf");
		contentTypes.put("exe", "application/octet-stream");
		contentTypes.put("bin", "application/octet-stream");
		contentTypes.put("zip", "application/zip");
		contentTypes.put("htm", "text/html");
		contentTypes.put("html", "text/html");
		contentTypes.put("gif", "image/gif");
		contentTypes.put("bmp", "image/bmp");
		contentTypes.put("jpg", "image/jpeg");
		contentTypes.put("jpeg", "image/jpeg");
		contentTypes.put("mpeg", "video/mpeg");
	}
	
	public int getAuditType() {
		return auditType;
	}

	public void setAuditType(int auditType) {
		this.auditType = auditType;
	}
	

	public int getAuditState() {
		return auditState;
	}

	public void setAuditState(int auditState) {
		this.auditState = auditState;
	}
	
	public int getPersonalType() {
		return personalType;
	}

	public void setPersonalType(int personalType) {
		this.personalType = personalType;
	}
	

	public int getTeacherId() {
		return teacherId;
	}

	public void setTeacherId(int teacherId) {
		this.teacherId = teacherId;
	}
	
	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}
	
	public int getStudentId() {
		return studentId;
	}

	public void setStudentId(int studentId) {
		this.studentId = studentId;
	}

	/*##################################################################################*/
	public String Add() throws Exception {
		// TODO Auto-generated method stub
		teacher=(Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
		if(teacher==null)return "input";
		if(uploadFile==null)return "returnAdd";
		
		/*
		 * 重命名文件
		 */
		String extendName=uploadFileFileName;
		/*截取扩展名*/
		if(extendName.lastIndexOf(".")==-1){
			extendName="";
		}else{
			extendName=extendName.substring(extendName.lastIndexOf("."));
		}
		/*用时间生成字符串*/
		java.text.DateFormat df=new java.text.SimpleDateFormat("yyMMddHHmmss");
		String dateString=df.format(new Date());
		/*用随机数和TeacherId生成字符串*/
		Random random=new Random();
		int randomNum = random.nextInt(911)*teacher.getTeacherId()%99+1;
		String randomString = String.valueOf(randomNum);
		if(randomString.length()==1){
			randomString="0"+randomString;
		}
		/*文件存储路径*/
		String newFileUrl="\\teachingFiles\\"+dateString+randomString+extendName;
		System.out.println("-------------------------------------"+newFileUrl+ServletActionContext.getServletContext().getRealPath("/"));
		/*将读入的文件写到指定路径中*/
		FileOutputStream fos=new FileOutputStream(ServletActionContext.getServletContext().getRealPath("/")+newFileUrl);
		FileInputStream fis=new FileInputStream(uploadFile);
		byte[] buffer=new byte[1024];
		int len=0;
		while((len=fis.read(buffer))>0){
		fos.write(buffer,0,len);
		}
		/*存文件*/
		if(teachSrcfileName.length()==0)teachSrcfileName=this.getUploadFileFileName();
		if(teachSrcFileContext.length()==0)teachSrcFileContext=this.getUploadFileFileName();
		teachingFiles=new TeachingFiles();
		teachingFiles.setTeachSrcFileUrl(newFileUrl);
		teachingFiles.setTeachSrcfileName(teachSrcfileName);
		teachingFiles.setTeachSrcFileContext(teachSrcFileContext);
		teachingFiles.setTeacher(teacher);
		teachingFiles.setDownloadtime(0);
		teachingFiles.setUploadTime(new Date());
		teachingFiles.setAuditing(0);
		boolean bl=this.getCommonService().saveObject(teachingFiles);
		if(!bl){
			return "error";
		}
		
		if(personalType==1){
			return "personalAdd";
		}
		
		return "add";
	}

	
	public String Show() throws Exception {
		// TODO Auto-generated method stub
		String hql="from TeachingFiles tf order by tf.uploadTime desc";
		pageModel=this.getCommonService().findPage(hql, null, currentPage, pageSize, "teachingFilesShow.action", "");
		if(pageModel.getList()!=null){
			for(int i=0;i<pageModel.getList().size();i++){
				teachingFiles=(TeachingFiles)pageModel.getList().get(i);
				/*设置日期格式*/
				java.text.DateFormat df=new java.text.SimpleDateFormat("yyyy-MM-dd");
				teachingFiles.setUploadTime(df.parse(df.format(teachingFiles.getUploadTime())));
			}
		}
		personalType=0;
		return "show";
	}
	
	public String Search() throws Exception {
		// TODO Auto-generated method stub
		String searchParameter="";
		String searchValue1=searchValue;
		String hql="";
		/*根据不同的personalType值不同执行不同的程序，跳到不同的result*/
		
		/*由于personalType等于3时是表示学生权限下载列表检索，由于查询语句比较特殊，所以要另写*/
		if(personalType!=3){
			hql="from TeachingFiles";
			if (like.equals("like")) {
				hql = hql + " as bf  where bf." + searchSelect + " like '%"+ searchValue1 + "%'";
			} else {
				hql = hql + " as bf  where bf." + searchSelect + " ='"+ searchValue1 + "'";
			}
		}
		
		
		if(personalType==1){
			hql=hql+" and bf.teacher.teacherId="+teacherId;
		}
		
		if(personalType==2){
			hql=hql+" and bf.auditing in (1,2)";
		}
		
		if(personalType==3){
			hql="select cd.teachingFiles from ClassDown";
			if (like.equals("like")) {
				hql = hql + " as cd  where cd.teachingFiles." + searchSelect + " like '%"+ searchValue1 + "%'";
			} else {
				hql = hql + " as cd  where cd.teachingFiles." + searchSelect + " ='"+ searchValue1 + "'";
			}
			
			hql=hql+" and cd.stclass.stclassId="+studentId;
		}
		
		String enSearchValue=java.net.URLEncoder.encode(searchValue1,"UTF-8");//URL编码转换
		
		/*searchParameter作为Dao层设置页面跳转参数*/
		searchParameter="&searchSelect="+searchSelect+"&searchValue="+enSearchValue+"&like="+like;
		
		if(personalType==1){
			searchParameter=searchParameter+"&teacherId="+teacherId;
			pageModel=this.getCommonService().findPage(hql, null, currentPage, pageSize,"teachingFilesSearch.action",searchParameter);
			return "personalSearch";
		}
		
		if(personalType==2){
			pageModel=this.getCommonService().findPage(hql, null, currentPage, pageSize,"teachingFilesSearch.action",searchParameter);
			return "personalAllSearch";
		}
		
		if(personalType==3){
			pageModel=this.getCommonService().findPage(hql, null, currentPage, pageSize,"teachingFilesSearch.action",searchParameter);
			return "studentSearch";
		}
	
		pageModel=this.getCommonService().findPage(hql, null, currentPage, pageSize,"teachingFilesSearch.action",searchParameter);
		
		return "search";
	}

	
	public String Detail() throws Exception {
		// TODO Auto-generated method stub
		teachingFiles=(TeachingFiles)this.getCommonService().findByIntId(teachSrcId, "gdut.jmxywz.been.TeachingFiles");
		
		String hqlSelected="select cd.stclass from ClassDown cd where cd.teachingFiles.teachSrcId="+teachSrcId;
		stclassSelectedList=this.getCommonService().findListByHql(hqlSelected, null);
		if(stclassSelectedList==null||stclassSelectedList.size()<1){
			System.out.println("=======================stclassSelectedList无值========================");
		}
		
		return "detail";
	}
	
	public String PreGrant() throws Exception {
		// TODO Auto-generated method stub
		teachingFiles=(TeachingFiles)this.getCommonService().findByIntId(teachSrcId, "gdut.jmxywz.been.TeachingFiles");
		/*分别查找选中和没有被选中的班级关联*/
		String hqlNotSelected="select stc from Stclass stc where stc not in (select cd.stclass from ClassDown cd where cd.teachingFiles.teachSrcId="+teachSrcId+")";
		stclassNotSelectedList=this.getCommonService().findListByHql(hqlNotSelected, null);
		
		String hqlSelected="select cd.stclass from ClassDown cd where cd.teachingFiles.teachSrcId="+teachSrcId;
		stclassSelectedList=this.getCommonService().findListByHql(hqlSelected, null);
		
		if(stclassNotSelectedList==null&&stclassSelectedList==null){
			return "error";
		}
		return "preGrant";
	}
	
	public String Grant() throws Exception {
		// TODO Auto-generated method stub
		/*重新授权之前，删除所有班级关联*/
		String deleteList="select cd from ClassDown cd where cd.teachingFiles.teachSrcId="+teachingFiles.getTeachSrcId();
		List olds=this.getCommonService().findListByHql(deleteList, null);
		for(int i=0;i<olds.size();i++){
			boolean d=this.getCommonService().deleteObject((ClassDown)olds.get(i));
			if(!d){
				return "error";
			}
		}
		TeachingFiles tf=new TeachingFiles();
		tf=(TeachingFiles)this.getCommonService().findByIntId(teachingFiles.getTeachSrcId(), "gdut.jmxywz.been.TeachingFiles");
		if(tf==null){
			return "error";
		}
		/*有选中的班级建立关联*/
		for(int i=1;i<stclassSelectedList.size();i++){
			ClassDown cd=new ClassDown();
			Stclass s=new Stclass();
			s=(Stclass)this.getCommonService().findByIntId(Integer.parseInt(stclassSelectedList.get(i).toString()), "gdut.jmxywz.been.Stclass");
			if(s==null){
				return "error";
			}
			
			cd.setTeachingFiles(tf);
			cd.setStclass(s);
			cd.setDownDescript(tf.getTeachSrcfileName());
			boolean bl=this.getCommonService().saveObject(cd);
			if(!bl){
				return "error";
			}
		}
		
		if(personalType==1)
			return "personalGrant";
		
		return "grant";
	}
	
	
	public String PreUpdate() throws Exception {
		// TODO Auto-generated method stub
		teachingFiles=(TeachingFiles)this.getCommonService().findByIntId(teachSrcId, "gdut.jmxywz.been.TeachingFiles");
		
		String hqlSelected="select cd.stclass from ClassDown cd where cd.teachingFiles.teachSrcId="+teachSrcId;
		stclassSelectedList=this.getCommonService().findListByHql(hqlSelected, null);
		return "preUpdate";
	}
	
	

	public String Update() throws Exception {
		// TODO Auto-generated method stub
		boolean bl=this.getCommonService().updateObject(teachingFiles);
		if(!bl){
			return "error";
		}
		
		if(personalType==1)
			return "personalUpdate";
		
		return "update";
	}
	

	public String Delete() throws Exception {
		// TODO Auto-generated method stub
		/*删除文件记录之前，先删除关联班级*/
		String deleteList="select cd from ClassDown cd where cd.teachingFiles.teachSrcId="+teachSrcId;
		List deleteClassDownList=this.getCommonService().findListByHql(deleteList, null);
		for(int i=0;i<deleteClassDownList.size();i++){
			boolean d=this.getCommonService().deleteObject((ClassDown)deleteClassDownList.get(i));
			if(!d){
				return "error";
			}
		}
		
		/*删除文件记录*/
		teachingFiles=(TeachingFiles)this.getCommonService().findByIntId(teachSrcId, "gdut.jmxywz.been.TeachingFiles");
		if(teachingFiles==null){
			return "error";
		}
		
		boolean bl=this.getCommonService().deleteObject(teachingFiles);
		if(!bl){
			return "error";
		}
		
		/*删除文件记录之后，删除文件*/
		try{
			File file=new File(ServletActionContext.getServletContext().getRealPath("/")+teachingFiles.getTeachSrcFileUrl());
			System.out.println("------------------------------"+ServletActionContext.getServletContext().getRealPath("/")+teachingFiles.getTeachSrcFileUrl());
			file.delete();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		if(personalType==1)
			return "personalDelete";
		
		return "delete";
	}

	public String PreAddClass() throws Exception {
		// TODO Auto-generated method stub
		//teachingFiles=(TeachingFiles)this.getCommonService().findByIntId(teachSrcId, "gdut.jmxywz.been.TeachingFiles");
		
		String hqlNotSelected="select stc from Stclass stc where stc not in (select cd.stclass from ClassDown cd where cd.teachingFiles.teachSrcId="+teachSrcId+")";
		stclassNotSelectedList=this.getCommonService().findListByHql(hqlNotSelected, null);
		return "preAddClass";
	}
	

	
	public String SearchClass() throws Exception {
		// TODO Auto-generated method stub
		/*在没有选中的班级中查询班级*/
		String hql="from Stclass";
		String searchParameter="";
		
		String searchValue1=searchValue;
		
		hql=hql+" as stc where stc not in (select cd.stclass from ClassDown cd where cd.teachingFiles.teachSrcId="+teachSrcId+") and stc.";
		
		if (like.equals("like")) {
			hql = hql + searchSelect + " like '%"+ searchValue1 + "%'";
		} else {
			hql = hql + searchSelect + " ='"+ searchValue1 + "'";
		}
		System.out.println("==============================="+hql);
		
		stclassNotSelectedList=this.getCommonService().findListByHql(hql, null);
		if(stclassNotSelectedList==null||stclassNotSelectedList.size()<1){
			System.out.println("====================stclassNotSelectedList为空====================");
		}
		
		return "searchClass";
	}

	public String AddClass() throws Exception {
		// TODO Auto-generated method stub
		teachingFiles=(TeachingFiles)this.getCommonService().findByIntId(teachSrcId, "gdut.jmxywz.been.TeachingFiles");
		/*stclasses不为空时依次增加被选中班级关联*/
		if(stclasses!=null){
			//System.out.println("===========================程序经过=========================="+teachSrcId+stclasses[0]);
			for(int i=0;i<stclasses.length;i++){
				if(stclasses[i]==0)continue;
				String hqlAddStclass="select stc from Stclass stc where stc.stclassId="+stclasses[i];
				stclassSelectedList=this.getCommonService().findListByHql(hqlAddStclass, null);
				Stclass stc=new Stclass();
				ClassDown cd=new ClassDown();
				boolean bl=false;
				if(stclassSelectedList!=null&&stclassSelectedList.size()>0){
					stc=(Stclass)stclassSelectedList.get(0);
					cd.setStclass(stc);
					cd.setTeachingFiles(teachingFiles);
					cd.setDownDescript(teachingFiles.getTeachSrcfileName());
					bl=this.getCommonService().saveObject(cd); 
				}
				if(!bl){
					return "error";
				}
			}
		}
		
		/*用types为all表示增加所有班级，用于与上面增加部分班级相区别*/
		if(types!=null&&types.equals("all")){
			
			String hqlNotSelected="select stc from Stclass stc where stc not in (select cd.stclass from ClassDown cd where cd.teachingFiles.teachSrcId="+teachSrcId+")";
			stclassNotSelectedList=this.getCommonService().findListByHql(hqlNotSelected, null);
			for(int i=0;i<stclassNotSelectedList.size();i++){
				ClassDown cd=new ClassDown();
				Stclass s=new Stclass();
				s=(Stclass)stclassNotSelectedList.get(i);
				
				if(s==null){
					return "error";
				}
				cd.setTeachingFiles(teachingFiles);
				cd.setStclass(s);
				cd.setDownDescript(teachingFiles.getTeachSrcfileName());
				boolean bl=this.getCommonService().saveObject(cd);
				if(!bl){
					return "error";
				}
			}
		}
		return "addClass";
	}

	
	public String DeleteClass() throws Exception {
		// TODO Auto-generated method stub
		teachingFiles=(TeachingFiles)this.getCommonService().findByIntId(teachSrcId, "gdut.jmxywz.been.TeachingFiles");
		/*用types条件判断是增加所有班级关联，还是单个班级关联*/
		if(types!=null&&types.equals("all")){
			String hqlSelected="select cd from ClassDown cd where cd.teachingFiles.teachSrcId="+teachSrcId;
			classDownSelectedList=this.getCommonService().findListByHql(hqlSelected, null);
			for(int i=0;i<classDownSelectedList.size();i++){
				ClassDown cd=new ClassDown();
				cd=(ClassDown)classDownSelectedList.get(i);
				boolean bl=this.getCommonService().deleteObject(cd); 
				if(!bl){
					return "error";
				}
			}
		}else{
			String hqlDeleteClass="select cd from ClassDown cd where cd.teachingFiles.teachSrcId="+teachSrcId+" and cd.stclass.stclassId="+stclassId;
			classDownSelectedList=this.getCommonService().findListByHql(hqlDeleteClass, null);
			if(classDownSelectedList!=null&&classDownSelectedList.size()>0){
				ClassDown cd=new ClassDown();
				cd=(ClassDown)classDownSelectedList.get(0);
				boolean bl=this.getCommonService().deleteObject(cd); 
				if(!bl){
					return "error";
				}
			}
		}
		
		return "deleteClass";
	}


	public String Download() throws Exception {
		// TODO Auto-generated method stub
		teachingFiles=(TeachingFiles)this.getCommonService().findByIntId(teachSrcId, "gdut.jmxywz.been.TeachingFiles");
		
		fileUrl=teachingFiles.getTeachSrcFileUrl();
		fileUrl=ServletActionContext.getRequest().getRealPath("")+fileUrl;//将相对路径装换为物理路径
		/*inputStream,fileDownSaveName,contentType对应struts.xml文件中的配置*/
		File file = new File(fileUrl);
		inputStream= new FileInputStream(file);
		
		fileDownSaveName=file.getName();
		contentType=this.getContentType(fileUrl);
		/*下载次数加1*/
		teachingFiles.setDownloadtime(teachingFiles.getDownloadtime()+1);
		//System.out.println("---------------------------"+teachingFiles.getDownloadtime());
		boolean bl=this.getCommonService().updateObject(teachingFiles);
		if(!bl){
			return "error";
		}
		return "download";
	}

	
	public String Audit() throws Exception {
		// TODO Auto-generated method stub
		/*审核，将审核状态存入数据库*/
		teachingFiles=(TeachingFiles)this.getCommonService().findByIntId(teachSrcId, "gdut.jmxywz.been.TeachingFiles");
		teachingFiles.setAuditing(auditType);
		boolean bl=this.getCommonService().updateObject(teachingFiles);
		if(!bl){
			return "error";
		}
		
		return "audit";
	}

	
	public String AuditSearch() throws Exception {
		// TODO Auto-generated method stub
		String hql="from TeachingFiles";
		String searchParameter="";
		
		/*为了实现状态检索后的跳页问题，必须进行状态判断和跳页传参*/
		if(auditState==4||auditState==5){
			hql=hql+" as bf order by bf.uploadTime";
		}else{
			hql=hql+" as bf where bf.auditing="+auditState+" order by bf.uploadTime";
		}

		searchParameter="&auditState="+auditState;
		
		pageModel=this.getCommonService().findPage(hql, null, currentPage, pageSize,"teachingFilesAuditSearch.action",searchParameter);
		return "auditSearch";
	}

/***
 * 教师个人课件列表
 * @return
 * @throws Exception
 */
	public String PersonalShow() throws Exception {
		// TODO Auto-generated method stub
		teacher=(Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
		if(teacher==null)
			return "input";
		teacherId=teacher.getTeacherId();
		
		String hql="from TeachingFiles tf where tf.teacher.teacherId="+teacherId+" order by tf.uploadTime desc"; 
		pageModel=this.getCommonService().findPage(hql, null, currentPage, pageSize, "teachingFilesPersonalShow.action", "");
		if(pageModel.getList()!=null){
			for(int i=0;i<pageModel.getList().size();i++){
				teachingFiles=(TeachingFiles)pageModel.getList().get(i);
				/*设置日期格式*/
				java.text.DateFormat df=new java.text.SimpleDateFormat("yyyy-MM-dd");
				teachingFiles.setUploadTime(df.parse(df.format(teachingFiles.getUploadTime())));
			}
		}
		
		personalType=1;
		return "personalShow";
	}

	
/***
 * 一般后台登陆者的课件下载列表
 * @return
 * @throws Exception
 */
	public String PersonalAllShow() throws Exception {
		// TODO Auto-generated method stub
		
		String hql="from TeachingFiles tf where tf.auditing=1 or tf.auditing=2 order by tf.uploadTime desc"; 
		pageModel=this.getCommonService().findPage(hql, null, currentPage, pageSize, "teachingFilesPersonalAllShow.action", "");
		if(pageModel.getList()!=null){
			for(int i=0;i<pageModel.getList().size();i++){
				teachingFiles=(TeachingFiles)pageModel.getList().get(i);
				/*设置日期格式*/
				java.text.DateFormat df=new java.text.SimpleDateFormat("yyyy-MM-dd");
				teachingFiles.setUploadTime(df.parse(df.format(teachingFiles.getUploadTime())));
			}
		}
		
		personalType=2;
		return "personalAllShow";
	}

	
/***
 * 学生课件下载，只有被授权班级中的学生才可以下载
 * @return
 * @throws Exception
 */
	public String StudentShow() throws Exception {
		// TODO Auto-generated method stub
		student=(Student)ActionContext.getContext().getSession().get(STUDENT_SESSION_KEY);
		if(student==null){
			return "input";
		}
		studentId=student.getStclass().getStclassId();
		
		/*查学生的所有可下载课件*/
		String hql="select cd.teachingFiles from ClassDown cd where cd.stclass.stclassId="+studentId+" order by cd.teachingFiles.uploadTime desc"; 
		pageModel=this.getCommonService().findPage(hql, null, currentPage, pageSize, "teachingFilesStudentShow.action", "");
		if(pageModel.getList()!=null){
			for(int i=0;i<pageModel.getList().size();i++){
				teachingFiles=(TeachingFiles)pageModel.getList().get(i);
				/*设置日期格式*/
				java.text.DateFormat df=new java.text.SimpleDateFormat("yyyy-MM-dd");
				teachingFiles.setUploadTime(df.parse(df.format(teachingFiles.getUploadTime())));
			}
		}
		//System.out.println("Test!++++++++++++++++++++++++++++++++++++++++++++++++++++++");
		personalType=3;
		return "studentShow";
	}
}
